using UnityEditor;
using UnityEngine;

namespace Editor
{
    public class AutoSetTexture : AssetPostprocessor
    {
        private void OnPreprocessTexture()
        {
            const string NOISE = "noise";
            const string INDEX = "index";
            const string DATA = "data";

            //自动设置类型;
            var importer = (TextureImporter) assetImporter;
            string assetPath = importer.assetPath;

            bool isNoise = assetPath.Contains(NOISE);
            bool isIndex = assetPath.Contains(INDEX);
            bool isData = assetPath.Contains(DATA);

            if (!isData && !isIndex && !isNoise) return;

            if (isIndex)
            {
                var setting = new TextureImporterSettings();
                importer.ReadTextureSettings(setting);
                setting.textureType = TextureImporterType.Default;
                setting.alphaSource = TextureImporterAlphaSource.None;
                setting.alphaIsTransparency = true;
                setting.wrapMode = TextureWrapMode.Clamp;
                setting.npotScale = TextureImporterNPOTScale.None;
                setting.readable = true;
                setting.mipmapEnabled = false;
                setting.filterMode = FilterMode.Point;
                importer.SetTextureSettings(setting);
            }

            if (isData)
            {
                var setting = new TextureImporterSettings();
                importer.ReadTextureSettings(setting);
                setting.textureType = TextureImporterType.SingleChannel;
                setting.alphaIsTransparency = false;
                setting.wrapMode = TextureWrapMode.Clamp;
                setting.npotScale = TextureImporterNPOTScale.None;
                setting.readable = true;
                setting.mipmapEnabled = false;
                setting.filterMode = FilterMode.Bilinear;
                setting.singleChannelComponent = TextureImporterSingleChannelComponent.Red;
                importer.SetTextureSettings(setting);
                // importer.textureType = TextureImporterType.SingleChannel;
                // importer.alphaIsTransparency = false;
                // importer.wrapMode = TextureWrapMode.Clamp;
                // importer.npotScale = TextureImporterNPOTScale.None;
                // importer.isReadable = true;
                // importer.mipmapEnabled = false;
                // importer.maxTextureSize = 2048;
                // importer.filterMode = FilterMode.Bilinear;
            }

            Debug.LogFormat("自动格式纠正 : {0}", assetPath);
        }
    }
}